package com.situ.onlinebookstore.back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.onlinebookstore.back.service.AdminService;
import com.situ.onlinebookstore.common.entity.Admin;
import com.situ.onlinebookstore.common.mapper.AdminMapper;
import com.situ.onlinebookstore.common.util.MD5Util;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {

    private final AdminMapper adminMapper;

    @Override
    public Admin login(Admin admin) throws Exception {
        //逻辑处理
        Admin sAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (sAdmin == null) {
            throw new Exception("用户名不存在！");
        }
        //密码是不是正确
        String md5Pwd = MD5Util.getDBMD5(admin.getPassword(), sAdmin.getSalt());
        if (!sAdmin.getPassword().equals(md5Pwd)) {
            throw new Exception("密码错误!");
        }
        //状态
        if (sAdmin.getStatus().equals(1)) {
            throw new Exception("账号已被禁用！");
        }
        //返回查询到的数据
        return sAdmin;
    }

    @Override
    public Admin autoLogin(Admin admin) {
        Admin sAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (sAdmin == null){
            return null;
        }
        if (!sAdmin.getPassword().equals(admin.getPassword())){
            return null;
        }
        if (sAdmin.getStatus() == 1){
            return null;
        }
        return sAdmin;
    }

    @Override
    public List search(Admin admin) {
        return adminMapper.select(admin);
    }

    @Override
    public PageInfo searchForPage(Integer page, Integer limit, Admin admin) {
        PageHelper.startPage(page, limit);
        List list = adminMapper.select(admin);
        return new PageInfo(list);
    }

    @Override
    public int edit(Admin admin) throws Exception {
        //查询管理员
        Admin sAdmin = adminMapper.selectById(admin.getId());
        if (sAdmin == null) {
            throw new Exception("管理员不存在");
        }
        //有可能只修改状态，也有可能只修改密码，也有可能修改电话邮箱姓名
        if (admin.getPassword() != null) {//123
            //需要对密码进行双重MD5盐加密
            String salt = UUID.randomUUID().toString().substring(0, 8);
            admin.setPassword(MD5Util.getDBMD5(admin.getPassword(), salt));
            admin.setSalt(salt);
        }
        //只修改状态
        if (admin.getStatus() != null) {
            if (sAdmin.getStatus().equals(admin.getStatus())) {
                throw new Exception("管理员状态不能重复修改！");
            }
        }
        //电话和邮箱是否允许重复

        //修改数据库
        return adminMapper.update(admin);
    }

    @Override
    public int add(Admin admin) throws Exception {
        //1-验证重复
        Admin sAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (sAdmin != null) {
            throw new Exception("用户名已被使用");
        }
        //电话 邮箱

        //2-密码加密
        String salt = UUID.randomUUID().toString().substring(0, 8);
        admin.setPassword(MD5Util.getDBMD5(admin.getPassword(), salt));
        admin.setSalt(salt);
        //3-保存到数据库
        return adminMapper.insert(admin);
    }

    @Override
    public int resetPwd(String oldPwd, String newPwd, Integer id) throws Exception {
        //        新密码与旧密码不能一致
        if (oldPwd.equals(newPwd)) {
            throw new Exception("新旧密码不能一样！");
        }

        //判断旧密码是否正确
        Admin sAdmin = adminMapper.selectById(id);
        String md5Pwd = MD5Util.getDBMD5(oldPwd, sAdmin.getSalt());
        if (! md5Pwd.equals(sAdmin.getPassword())) {
            throw new Exception("旧密码错误");
        }


        //新密码加密
        newPwd = MD5Util.getDBMD5(newPwd, sAdmin.getSalt());
        sAdmin.setPassword(newPwd);
        //修改数据库
        return adminMapper.update(sAdmin);
    }
}
